package com.zecan.blog.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.zecan.blog.rabbitmq.event.FollowEvent;
import com.zecan.blog.rabbitmq.event.ChatRoomEvent;
import com.zecan.blog.rabbitmq.event.SendEmailEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * rabbitmq 生产者
 * @ClassName BlogMqProduct
 * @Description
 * @date 2022/11/24 19:25
 * @Version 1.0
 */
@Component
@Slf4j
public class BlogMqProducer {


    private RabbitTemplate rabbitTemplate;


    /**
     * rabbitmq 聊天室发送消息的方法
     * @param exchange 交换机
     * @param routingKey 路由key
     * @param chatRoomEvent 存储信息event
     */
    public void sendChatMessage(String exchange, String routingKey, ChatRoomEvent chatRoomEvent) {
        try {
            // 发送消息  将数据装换为json
            rabbitTemplate.convertAndSend(exchange,routingKey, JSON.toJSONString(chatRoomEvent));
        } catch (Exception e) {
            log.error("当前event出现异常 ----->: " + e);
        }
    }


    /**
     * rabbitmq 发送邮箱
     * @param exchange 交换机
     * @param routingKey 路由
     * @param sendEmailEvent 发送的内容
     */
    public void sendEmailMessage(String exchange, String routingKey, SendEmailEvent sendEmailEvent) {
        try {
            // 发送消息  将数据装换为json
            rabbitTemplate.convertAndSend(exchange,routingKey, JSON.toJSONString(sendEmailEvent));
        } catch (Exception e) {
            log.error("当前event出现异常 ----->: " + e);
        }
    }

    /**
     * rabbitmq 发送邮箱
     * @param exchange 交换机
     * @param routingKey 路由
     * @param followEvent 发送的内容
     */
    public void sendFollowMessage(String exchange, String routingKey, FollowEvent followEvent) {
        try {
            this.rabbitTemplate.convertAndSend(exchange, routingKey, JSON.toJSONString(followEvent));
        } catch (Exception var5) {
            log.error("当前event出现异常 ----->: " + var5);
        }

    }
    /**
     * rabbitmq 发送邮箱
     * @param exchange 交换机
     * @param routingKey 路由
     * @param followEvent 发送的内容
     */
    public void sendUnFollowMessage(String exchange, String routingKey, FollowEvent followEvent) {
        try {
            this.rabbitTemplate.convertAndSend(exchange, routingKey, JSON.toJSONString(followEvent));
        } catch (Exception var5) {
            log.error("当前event出现异常 ----->: " + var5);
        }

    }
}
